<template>
  <div>
    <div v-if="isPageShow" class="wrap">
      <!-- header头部 -->
      <!-- <m-header
        :activeIndex="0"
        :backcolor="'#1968FA'"
        :color="'#fff'"
        opacity="1"
        hocolor="#fff"
        filter="blur(0px)"
        @hanldeGetValue="hanldeGetInputValue"
      ></m-header> -->
      <div style="height: 50px"></div>
      <div class="container" style="width: 100%; max-width: 1200px">
        <div class="nav">首页/搜索</div>
        <div
          class="content"
          v-if="$store.state.searchDataList.length > 0"
          v-loading="$store.state.isLoading"
        >
          <div
            @click="hanldePermissionVerification(item)"
            class="cont-box"
            v-for="item in $store.state.searchDataList"
            :key="item.allocationsId"
          >
            <div class="title co-height">
              <div>{{ item.eventName }}</div>
              <div>{{ item.createTime }}</div>
            </div>
            <div class="con co-height">
              {{ item.title }}
            </div>
          </div>
        </div>
        <div class="content" v-else v-loading="$store.state.isLoading">
          <div slot="empty" class="empty">
            <img src="../../assets/images/empty.png" alt="" />
            <span class="text"> 暂无数据 </span>
          </div>
        </div>
        <div class="clearfix" v-if="$store.state.searchDataList.length > 0">
          <el-pagination
            class="pull-right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total,prev,pager,next,sizes,jumper"
            :total="$store.state.searchTotalCount"
          ></el-pagination>
        </div>
      </div>
      <!-- footer页脚 -->
      <!-- <m-fotter /> -->
    </div>
  </div>
</template>

<script>
import { STATUS_CODE_OK } from "@/config";
import { homeSearch, permissionVerification } from "@/api/homePage.js";
// import MHeader from "@/components/MHeader.vue";
// import MFotter from "@/components/MFotter.vue";
export default {
  name: "SearchPage",
  data() {
    return {
      currentPage: 1, // 页数
      pageSize: 20, // 条数
      totalCount: 0, // 总条数
      isPageShow: true, //页面是否显示
      dataList: [],
      searchValue: "",
    };
  },
  async created() {
    await this.handleHomeSearch();
  },
  mounted() {
    // 绑定监听事件
    window.addEventListener("keydown", this.keyDown);
  },
  destroyed() {
    //销毁事件
    window.removeEventListener("keydown", this.keyDown, false);
  },
  methods: {
    // 点击回车键登录
    keyDown(e) {
      // 点击回车键执行搜索方法 enter键的ASCII是13
      if (e.keyCode === 13) {
        this.handleHomeSearch(); // 定义的登录方法
      }
    },
    checkUrlStatus(code) {
      const urlMessages = {
        1775402307172499457: "course", // 金职大讲堂
        1775402446075265026: "resourcesDetails", // 赛项资源
        1775402520624824321: "oneXbook", // 1+X证书资源
        1775402577172430850: "skills", // 技能培训
        1775402630247153666: "industry", // 行业资源
        default: "homePage",
      };
      const message =
        code.eveMoId == 1775402307172499457
          ? urlMessages[1775402307172499457]
          : code.eveMoId == 1775402446075265026
          ? urlMessages[1775402446075265026]
          : code.eveMoId == 1775402520624824321
          ? urlMessages[1775402520624824321]
          : code.eveMoId == 1775402577172430850
          ? urlMessages[1775402577172430850]
          : code.eveMoId == 1775402630247153666
          ? urlMessages[1775402630247153666]
          : urlMessages.default;
      this.$router.push({
        name: message,
        params: { row: code },
      });
    },
    async hanldePermissionVerification(item) {
      let { data } = await permissionVerification({
        type: item.type,
        groupType: item.groupType,
        allocationId: item.id,
      });
      if (data.code == STATUS_CODE_OK) {
        if (data.data) {
          // 判断资源类型跳转页面
          this.checkUrlStatus(item);
          sessionStorage.removeItem("resourcesTab");
        } else {
          this.$notify.warning({
            title: "提示",
            message: "暂无浏览权限,请联系管理员",
          });
          return;
        }
      }
    },
    // hanldeGetInputValue(value) {
    //   this.searchValue = value;
    //   console.log(this.searchValue);
    //   this.handleHomeSearch();
    // },
    async handleHomeSearch() {
      let { data } = await homeSearch({
        searchValue: this.$store.state.searchValue,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
      if (data.code == STATUS_CODE_OK) {
        // sessionStorage.setItem(
        //   "searchDataList",
        //   JSON.stringify(data.data.records)
        // );
        // sessionStorage.setItem("searchTotalCount", data.data.total);
        this.$store.dispatch("setSearchData", data.data.records);
        this.$store.dispatch("setSearchTotal", data.data.total);
      }
    },
    // 分页页数改变事件
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.handleHomeSearch();
    },
    // 分页当前页改变事件
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handleHomeSearch();
    },
  },
  // components: {
  //   MHeader,
  //   MFotter,
  // },
};
</script>

<style scoped lang="scss">
$bg-color: #fff;
.content-overflow-scroll {
  height: 600px;
  overflow: auto;
  li {
    height: 50px;
    line-height: 50px;
    margin-bottom: 10px;
    border: 1px solid #ececec;
    text-align: center;
  }
}
::v-deep .el-dialog__wrapper {
  overflow-y: hidden;
}
::v-deep .el-dialog__body {
  display: flex;
  justify-content: space-evenly;
}
.empty {
  display: flex;
  padding: 30px;
  height: 600px;
  flex-direction: column;
  // justify-content: space-evenly;
  align-items: center;
  img {
    width: 250px;
    height: 250px;
    margin: 70px 0px 0px 28px;
  }
  .text {
    color: #7c7e80;
  }
}
.dialog-box {
  .pdf-box {
    overflow-y: auto;
    max-height: 600px;
    display: flex;
    width: 600px;
    flex-direction: column;
    align-items: center;
    .pdf {
      width: 100%;
      border: 1px solid #000;
      margin-bottom: 10px;
    }
  }
  .dialog-video {
    display: flex;
    align-items: center;
    justify-content: space-evenly;
  }
}
.wrap {
  background-color: $bg-color;
  .container {
    padding-bottom: 40px;
    .nav {
      color: #7c7e80;
      padding: 20px 20px 10px;
    }
    .content {
      box-sizing: border-box;
      padding: 10px 30px;
      margin: 0px auto 20px;
      width: 100%;
      max-width: 1200px;
      background: $bg-color;
      .cont-box {
        border-bottom: 1px dashed #eee;
        padding: 10px 20px;
        .co-height {
          padding: 10px 0px;
        }
        .title {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
      }
    }
  }
}
</style>
